package com.yunyao.dao.service.bamu.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunyao.common.constant.enums.DataDeletedStatus;
import com.yunyao.common.utils.ObjectToMapConverter;
import com.yunyao.dao.mapper.BamuSpReconciliationOrderMaterialMapper;
import com.yunyao.dao.model.*;
import com.yunyao.dao.service.bamu.*;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 供应商对账订单材料表 服务实现类
 * </p>
 *
 * @author xingjishuai
 * @since 2024-07-02
 */
@Service
public class BamuSpReconciliationOrderMaterialServiceImpl extends ServiceImpl<BamuSpReconciliationOrderMaterialMapper, BamuSpReconciliationOrderMaterial> implements IBamuSpReconciliationOrderMaterialService {
@Autowired
private IBamuSpReconciliationOrderService iBamuSpReconciliationOrderService;
    /**
     * 根据对账ID查询材料列表
     * <p>
     * 该方法用于根据对账ID查询{@link BamuSpReconciliationOrderMaterial}类型的材料列表
     * 如果对账ID为空或空白，则返回一个空列表
     * 否则，查询条件包括对账ID和材料未被删除的状态
     *
     * @param reconciliationId 对账ID，用于查询特定对账单的材料
     * @return 返回{@link BamuSpReconciliationOrderMaterial}类型的材料列表，如果查询不到任何材料，则返回一个空列表
     */
    @Override
    public List<BamuSpReconciliationOrderMaterial> listByReconciliationId(String reconciliationId) {
        // 检查对账ID是否为空或空白，如果是，则直接返回一个空列表
        if (StringUtils.isBlank(reconciliationId)) {
            return List.of();
        }
        // 构建查询条件，包括对账ID和材料未被删除的状态
        LambdaQueryWrapper<BamuSpReconciliationOrderMaterial> wrapper =
                new LambdaQueryWrapper<BamuSpReconciliationOrderMaterial>().eq(BamuSpReconciliationOrderMaterial::getReconciliationId, reconciliationId)
                        .eq(BamuSpReconciliationOrderMaterial::getIsDeleted,
                                DataDeletedStatus.NO.getType());
        // 执行查询并返回结果
        return list(wrapper);
    }

    @Override
    public BigDecimal getSupplyNumByContractId(String contractId) {
        return baseMapper.getSupplyNumByContractId(contractId);
    }


    @Override
    public BigDecimal getComfirmSupplyNumByContractId(String contractId) {
        return baseMapper.getComfirmSupplyNumByContractId(contractId);
    }


    @Override
    public BigDecimal getIncludingTaxTotalPriceByContractId(String contractId) {
        return baseMapper.getIncludingTaxTotalPriceByContractId(contractId);
    }


    @Override
    public BigDecimal getReconciliationPriceByContractId(String contractId) {
        return baseMapper.getReconciliationPriceByContractId(contractId);
    }

    @Override
    public void deletedByReconciliationIdList(List<String> posupplyIdList) {
        // 检查对账ID列表是否为空，如果为空则直接返回空列表
        if (CollectionUtils.isEmpty(posupplyIdList)) {
            return;
        }

        LambdaQueryWrapper<BamuSpReconciliationOrder> wrapper1 = new LambdaQueryWrapper<BamuSpReconciliationOrder>()
                .in(BamuSpReconciliationOrder::getSupplyId, posupplyIdList);
        List<BamuSpReconciliationOrder> bamuSpReconciliationOrderList = iBamuSpReconciliationOrderService.list(wrapper1);

        // 提取对账订单的ID列表
        List<String> reconciliationOrderIds = bamuSpReconciliationOrderList.stream()
                .map(BamuSpReconciliationOrder::getId)
                .collect(Collectors.toList());

        // 创建查询条件封装Lambda表达式，用于筛选特定条件的对账结算单
        LambdaUpdateWrapper<BamuSpReconciliationOrderMaterial> wrapper =
                new LambdaUpdateWrapper<BamuSpReconciliationOrderMaterial>()
                        // 条件1：对账ID在给定的列表中
                        .in(BamuSpReconciliationOrderMaterial::getReconciliationOrderId,
                                reconciliationOrderIds)
                        .eq(BamuSpReconciliationOrderMaterial::getIsDeleted,
                                DataDeletedStatus.NO.getType())
                        .set(BamuSpReconciliationOrderMaterial::getIsDeleted,
                                DataDeletedStatus.YES.getType());
        update(wrapper);
    }

}
